package com.hsy.leancloudchat.main

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import cn.leancloud.LCUser
import cn.leancloud.im.v2.LCIMConversation
import com.hsy.leancloudchat.LeanCloudHelper
import com.hsy.leancloudchat.databinding.ItemConversationRequestBinding

class ConversationRequestAdapter(lifecycleOwner: LifecycleOwner) :
    RecyclerView.Adapter<ConversationRequestAdapter.ConversationRequestViewHolder>() {
    private val mLifeCycleOwner = lifecycleOwner

    private var acceptListener: ((Int) -> Unit)? = null
    fun setAcceptListener(listener: (Int) -> Unit) {
        acceptListener = listener
    }

    var conversationRequestList = mutableListOf<LCIMConversation>()
        @SuppressLint("NotifyDataSetChanged")
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ConversationRequestViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        return ConversationRequestViewHolder(
            ItemConversationRequestBinding.inflate(
                layoutInflater,
                parent,
                false
            )
        )
    }

    override fun getItemCount(): Int {
        return conversationRequestList.size
    }

    override fun onBindViewHolder(holder: ConversationRequestViewHolder, position: Int) {
        holder.bind(position)
    }

    inner class ConversationRequestViewHolder(private val binding: ItemConversationRequestBinding) :
        RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("NotifyDataSetChanged")
        fun bind(position: Int) {
            val mConversation = conversationRequestList[position]
            binding.apply {
                val foundUserLiveData = MutableLiveData<LCUser?>()
                val friendId = if (mConversation.members[0] == LCUser.currentUser().objectId) {
                    mConversation.members[1]
                } else {
                    mConversation.members[0]
                }
                LeanCloudHelper.getInstance().getUserInBackGround(
                    friendId,
                    foundUserLiveData
                )
                foundUserLiveData.observe(mLifeCycleOwner) {
                    tvName.text = it?.username
                }

                btnReject.setOnClickListener {
                    conversationRequestList.remove(mConversation)
                    notifyDataSetChanged()

                }
                btnAccept.setOnClickListener {
                    conversationRequestList.remove(mConversation)
                    notifyDataSetChanged()
                    LeanCloudHelper.getInstance().sendMessage(
                        mConversation,
                        "我们已经成为好友了",
                        context = it.context
                    )
                    acceptListener?.invoke(conversationRequestList.size)
                }
            }
        }
    }
}